package com.demo.supplier.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页响应结果类
 * 
 * @author demo
 */
@Data
@ApiModel("分页响应结果")
public class Pages<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("数据列表")
    private List<T> records;

    @ApiModelProperty("总记录数")
    private Long total;

    @ApiModelProperty("当前页码")
    private Long current;

    @ApiModelProperty("每页大小")
    private Long size;

    @ApiModelProperty("总页数")
    private Long pages;

    public Pages() {
    }

    public Pages(List<T> records, Long total, Long current, Long size) {
        this.records = records;
        this.total = total;
        this.current = current;
        this.size = size;
        this.pages = total % size == 0 ? total / size : total / size + 1;
    }

    /**
     * 根据MyBatis-Plus的IPage对象构建Pages对象
     */
    public static <T> Pages<T> of(IPage<T> page) {
        return new Pages<>(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    /**
     * 根据数据构建Pages对象
     */
    public static <T> Pages<T> of(List<T> records, Long total, Long current, Long size) {
        return new Pages<>(records, total, current, size);
    }
}